package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.entity.Company;

import java.util.List;
import java.util.Map;

@Mapper
public interface CompanyMapper extends BaseMapper<Company> {

    @Select("SELECT c.* FROM companies c " +
            "WHERE c.id IN (" +
            "    SELECT DISTINCT company_id FROM user_role_company_relations WHERE user_id = #{userId}" +
            ") " +
            "ORDER BY c.created_at DESC")
    List<Company> selectCompaniesByUserId(@Param("userId") Long userId);
    
    /**
     * 查询用户在各公司的权限信息
     */
    @Select("SELECT c.id, c.company_name, c.company_code, c.status, " +
            "urcr.is_active, urcr.relationship_type, r.role_name " +
            "FROM companies c " +
            "INNER JOIN user_role_company_relations urcr ON c.id = urcr.company_id " +
            "LEFT JOIN roles r ON urcr.role_id = r.id AND r.company_id = urcr.company_id " +
            "WHERE urcr.user_id = #{userId} AND urcr.is_active = 1 " +
            "ORDER BY c.created_at DESC")
    List<Map<String, Object>> selectUserCompanyPermissions(@Param("userId") Long userId);
}


